package com.zmyuan.quartz.db.demo;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Properties;

import static org.quartz.JobBuilder.newJob;

/**
 * Created by zhudebin on 16/7/19.
 */
public class Demo1 {

    public static void main(String[] args) throws Exception {

        Properties properties = new Properties();
        properties.load(Demo1.class.getResourceAsStream("/quartz.properties"));
        StdSchedulerFactory schedFact = new StdSchedulerFactory(properties);


        Scheduler sched = schedFact.getScheduler();


        sched.start();

        // define the job and tie it to our HelloJob class
        JobDetail job = newJob(HelloJob.class)
                .withIdentity("myJob", "group1")
                .usingJobData(HelloJob.IDX, "1")
                .build();

        // 触发时间点
        SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder
                .simpleSchedule()
                .withRepeatCount(10)
                .withIntervalInSeconds(2)
//                .withMisfireHandlingInstructionIgnoreMisfires()
//                .withMisfireHandlingInstructionFireNow() // 立即执行,然后按照规则继续 总次数减少
//                .withMisfireHandlingInstructionNextWithExistingCount()
//                .withMisfireHandlingInstructionNextWithRemainingCount()
                .withMisfireHandlingInstructionNowWithExistingCount()  // 总次数不变
//                .withMisfireHandlingInstructionNowWithRemainingCount()
                ;
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .startNow()
                .withSchedule(simpleScheduleBuilder).build();



        // Tell quartz to schedule the job using our trigger

        sched.scheduleJob(job, trigger);

        TriggerKey triggerKey = TriggerKey.triggerKey("trigger1", "group1");
        for(int i=0; i< 100; i++) {
            String summary = sched.getMetaData().getSummary();

            System.out.println(summary);

            Thread.sleep(5000);
            Trigger.TriggerState state = sched.getTriggerState(triggerKey);
            if((state == Trigger.TriggerState.ERROR)
                    || (state == Trigger.TriggerState.COMPLETE)
                    || (state == Trigger.TriggerState.NONE)
                    ) {
                sched.shutdown();
            }
            System.out.println(state.name());
            if(i % 2 == 0) {
                if(state != Trigger.TriggerState.PAUSED) {
                    sched.pauseTrigger(triggerKey);
                    System.out.println("------暂停作业");
                } else {
                    System.out.println("------ ++ ------" + state);
                }
            } else {
                if(state == Trigger.TriggerState.PAUSED) {
                    sched.resumeTrigger(triggerKey);
                    System.out.println("------恢复作业-----");
                } else {
                    System.out.println(state);
                    System.out.println("------ ?? ------" + state);
                }
            }
        }

//        sched.shutdown();

    }

}


